<?php
defined('_JEXEC') or die( 'Restricted access' );
//define('NUSAVIEW_IMAGE',    JURI::root().'images'.'/'.'nusaview'.'/');
JTable::addIncludePath(JPATH_COMPONENT.DS.'tables');

jimport('joomla.application.component.model');
class NVModelMap extends JModel
{

	var $_data = null;


	function _buildQuery()
	{

		$query = ' SELECT  id_pariwisata, name,latitude,longitude,keterangan ' .
				' FROM #__nusaview ';
		return $query;
	}

	function store($data)
	{
		$row =& JTable::getInstance('nusaview', 'Table');

		if (!$row->bind($data)) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}
		if (!$row->store()) {
			$this->setError($this->_db->getErrorMsg());
			return false;
		}


		return true;

	}

	function getData()
	{
		$query=$this->_buildQuery();
		$this->_db->setQuery($query);
		$this->_data = $this->_db->loadRow();
		return $this->_data;

	}

	function getJSON(){

		$query=$this->_buildQuery();
		$this->_db->setQuery($query);

		$data=$this->_db->loadObjectList();
		$i=1;
		$numrow=count($data);
		$json="{\n\"nusaview\": [\n";
		foreach ( $data as $row ) {
			$json.="{ ";
			$json.=" nama: \"".$row->name."\",\n";
    		$json.=" URL: \"".JURI::root()."index.php?option=com_nusaview&id=".$row->id_pariwisata."\",\n";
    		$json.=" Posisi: \"".$row->latitude.",".$row->longitude."\",\n";
    		$json.=" Keterangan: \"".$row->keterangan."\"\n";
    		$json.=" }";
			if($numrow>$i)
				{
					$i++;
					$json.=",\n";
				}
			else
				$json.="\n";
		}
		$json.="]\n}";
  		return $json;
 }

function getRDF(){

		$query=$this->_buildQuery();
		$this->_db->setQuery($query);

		$data=$this->_db->loadObjectList();
		$i=1;
		$numrow=count($data);
		$rdf='<?xml version="1.0"?>' .
				'<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" ' .
				'xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" ' .
				'xmlns="http://purl.org/rss/1.0/">';
		foreach ( $data as $row ) {
			$rdf.='<item rdf:about="'.JURI::root().'index.php?option=com_nusaview&amp;id='.$row->id_pariwisata.'">';
			$rdf.='<title>'.$row->name.'</title>';
    		$rdf.='<link>'.JURI::root().'index.php?option=com_nusaview&amp;id='.$row->id_pariwisata.'</link>';
    		$rdf.='<description>'.$row->keterangan.'</description>';
    		$rdf.='<geo:lat>'.$row->latitude.'</geo:lat>';
    		$rdf.=' <geo:long>'.$row->longitude.'</geo:long>';
    		$rdf.='</item>';

		}
		$rdf.='</rdf:RDF> ';
  		return $rdf;
 }

 function getKML(){

			$query=$this->_buildQuery();
		$this->_db->setQuery($query);

		$data=$this->_db->loadObjectList();
		$i=1;
		$numrow=count($data);
		$kml='<?xml version="1.0" encoding="UTF-8"?>' .
				'<kml xmlns="http://www.opengis.net/kml/2.2"> ' .
				'<Document> ' .
				'<name>'.JURI::base().'</name> ' .
				'<description>Nusantara View KML</description>
';
		foreach ( $data as $row ) {
			$kml.=' <Placemark>';
			$kml.='<name>'.$row->name.'</name>';
    		$kml.='<description>'.$row->keterangan.'</description>';
    		$kml.='<Point><coordinates>'.$row->longitude.','.$row->latitude.',0</coordinates></Point>';
    		$kml.='</Placemark>';

		}
		$kml.='</Document></kml> ';
  		return $kml;
 }

}


?>